package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 秘境降妖任务
 *
 * @author: 舟小亢
 * @date: 2023-08-12 22:03
 */
public class SecretAreaTaskImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(SecretAreaTaskImpl.class);

    public SecretAreaTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "秘境降妖";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【秘境降妖任务】开始！");

        // 点击活动
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_活动)) {
            return false;
        }

        // 找到日常活动并点击
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_日常活动)) {
            return false;
        }

        // 点击参加秘境任务
        MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, 1000);

        for (int i = 0; i < ParamConstants.UNIFY_TIMES; i++) {
            // 先检查是否完成 todo 双倍活跃时候的截图也需要
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_已完成, 2, 0.95);
            if (pic.isFinded()) {
                // 摁ESC，退出
                ThreadUtil.sleep1();
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                LOG.info("【秘境降妖任务】已完成！");
                return false;
            }
            // 如果未完成
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_秘境图标, 2);
            if (pic.isFinded()) {
                break;
            }
            // 如果没找到下拉滑动屏幕寻找图标
            MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.ATTEND_TASK_DXY.getX(),
                pic.getPicY() + Coord.ATTEND_TASK_DXY.getY());

        // 点击接受任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_接受任务)) {
            return false;
        }

        // 拖拽至最左边(拖拽2次，增加成功率)
        ThreadUtil.sleep2();
        MouseUtil.dragMove(hwnd_1, Coord.SECRET_DRAG_XY.getX(), Coord.SECRET_DRAG_XY.getY(), 2000, 0);
        MouseUtil.dragMove(hwnd_1, Coord.SECRET_DRAG_XY.getX(), Coord.SECRET_DRAG_XY.getY(), 2000, 0);

        // 如果是本周第一次进入，需要选择去哪个秘境，默认第一个
        pic = FuzzyPicUtil.findPicM(hwnd_0,
                new String[]{PicConstants.秘境降妖_进入, PicConstants.秘境降妖_第一关, PicConstants.秘境降妖_第一关1});
        if (!pic.isFinded()) {
            return false;
        }
        //第一次进入
        if (pic.getFindResult()[0] == 1) {
            LOG.info("本周第一次进入秘境降妖!");
            // 先点击进入
            MouseUtil.lClickRandom(hwnd_1, pic.getPicX(), pic.getPicY());
            // 再点击确定
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_确认);
        }

        // 找到并点击第一关
        pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.秘境降妖_第一关, PicConstants.秘境降妖_第一关1}, 5);
        if (!pic.isFinded()) {
            return false;
        }

        // 游戏更新：换成更方便的继续挑战
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_继续挑战, 5, 0.8d);

        // 开始战斗
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_战斗, 5, 0.6d);
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_继续战斗, 5);
        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        int totalRound = 500;
        // 开始战斗
        for (int i = 0; i < totalRound; i++) {
            LOG.info("【秘境任务】检查完成情况,第[{}]次", i);
            if (i == totalRound - 1) {
                // 循环结束后还是没有完成，则直接退出秘境，点击离开
                int cnt = 0;
                while (cnt++ < 100) {
                    if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_离开, 2)) {
                        LOG.info("【秘境任务】循环结束后还是没有完成，则直接退出秘境，点击离开！");
                        return true;
                    }
                    ThreadUtil.sleep(3000);
                }
            }
            // 找到活动
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 2, 0.95).isFinded()) {
                LOG.info("【秘境任务】第一次找到活动图标！");
                // 双重查找,保证准确找到
                ThreadUtil.sleep(10000);
                if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_秘境小图标, 2).isFinded()
                        && FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 2, 0.95).isFinded()) {
                    LOG.info("【秘境任务】战斗死亡次数过多，退出！");
                    return true;
                }
            }

            // 找到已通关并离开
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_通关, 2).isFinded()) {
                // 循环点击，直到确认完全退出
                for (int j = 0; j < 15; j++) {
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_离开, 2);
                    if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 2).isFinded()) {
                        LOG.info("【秘境任务】完成所有战斗，找到活动，已确认成功退出！");
                        return true;
                    }
                    LOG.info("【秘境任务】循环确认是否已成功退出，第{}次", j);
                }
            }

            // 战斗中
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_战斗中, 10).isFinded()) {
                LOG.info("【秘境任务】战斗中，睡眠10秒！");
                ThreadUtil.sleep(10000);
                continue;
            }

            // 检查初始状态
            if (!secretIniState(3)) {
                LOG.warn("【秘境任务】初始化秘境状态失败，继续下一轮检查！");
                continue;
            }

            // 如果有取消按钮，先点击取消掉
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_退出_取消, 3);

            // 找到后点击继续战斗
            LOG.info("【秘境任务】继续点击战斗！");
            if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_继续战斗, 3).isFinded()) {
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_战斗, 3);
            }
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.秘境降妖_继续战斗, 3);
        }
        LOG.info("【秘境任务】已完成！");
        return true;
    }

    /**
     * 秘境单独的刷新状态
     *
     * @return
     */
    private boolean secretIniState(int cnt) throws Exception {
        cnt = cnt <= 0 ? 1 : cnt;
        for (int i = 0; i < cnt; i++) {
            // 循环摁ESC，直到找到猴子照片
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.秘境降妖_秘境小图标, 3);
            if (pic.isFinded()) {
                return true;
            }
            KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
        }
        return false;
    }
}
